package cn.elves.model.auth.service.impl;

import cn.elves.model.auth.entity.po.User;
import cn.elves.model.auth.entity.po.UserRoleRelation;
import cn.elves.model.auth.mapper.UserMapper;
import cn.elves.model.auth.service.UserRoleRelationService;
import cn.elves.model.auth.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author pc
 * @description 针对表【tb_user(用户表)】的数据库操作Service实现
 * @createDate 2024-05-26 18:34:43
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Resource
    private UserRoleRelationService userRoleRelationService;

    @Override
    public void allocationRole(Long userId, Set<Long> roleIdSet) {
        userRoleRelationService.remove(new QueryWrapper<UserRoleRelation>().eq("user_id", userId));

        List<UserRoleRelation> userRoleRelationList = roleIdSet.stream().map(roleId -> {
            UserRoleRelation userRoleRelation = new UserRoleRelation();
            userRoleRelation.setUserId(userId);
            userRoleRelation.setRoleId(roleId);
            return userRoleRelation;
        }).collect(Collectors.toList());
        userRoleRelationService.saveBatch(userRoleRelationList);

    }
}




